package com.iqiyi.pps.epg.core.rpc.statistic;

import com.alibaba.fastjson.JSONObject;
import com.iqiyi.pps.epg.api.model.web.query.TFilter;
import com.iqiyi.pps.epg.api.model.web.query.TPage;
import com.iqiyi.pps.epg.api.model.web.query.TResponse;
import com.iqiyi.pps.epg.api.model.web.statistic.*;
import com.iqiyi.pps.epg.api.server.web.statistic.StatisticWebService;
import com.iqiyi.pps.epg.core.model.statistic.*;
import com.iqiyi.pps.epg.core.service.statistic.*;
import com.iqiyi.pps.epg.core.utils.ApplicationContextUtil;
import com.iqiyi.pps.epg.core.utils.Constants;
import com.iqiyi.pps.epg.core.utils.WriteSystemLogUtil;
import org.apache.thrift.TException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Timestamp;
import java.util.List;


public class StatisticWebServiceImpl implements StatisticWebService.Iface {

    private StatisticOcrNewDataService getStatisticOcrNewDataService() {
        return WriteSystemLogUtil.getInstance().getStatisticOcrNewDataService();
    }

    private StatisticAutoNewDataService getStatisticAutoNewDataService() {
        return WriteSystemLogUtil.getInstance().getStatisticAutoNewDataService();
    }

    public static Logger logger = LoggerFactory.getLogger(StatisticWebServiceImpl.class);

    // 制作相关数据存入数据库
    @Override
    public long saveProduceData(TStatisticProduceData entity) throws TException {
        if (null == entity)
            return -1;
        StatisticProduceData statisticProduceData = null;
        try {
            StatisticService statisticService = (StatisticService) ApplicationContextUtil.getBean(StatisticService.class);
            if (entity.getId() > 0) {
                statisticProduceData = statisticService.getProduceData(entity.getId());
                if (statisticProduceData == null) {
                    return -1;
                }
            }else{
                statisticProduceData = new StatisticProduceData();
            }
            statisticProduceData.setProgramId(entity.getProgramId());
//            if (StringUtils.isNotBlank(entity.getEnterLegoTime())) {
//                statisticService.updateEnterLegoTime(entity.getProgramId(), new Timestamp(System.currentTimeMillis())); // 用本服务器时间做为当前直转点进入lego时间
//                return true;
//            }
            statisticProduceData.setProgramName(entity.getProgramName());
            statisticProduceData.setProgramMinutes(Float.valueOf(entity.getProgramMinutes()));
            statisticProduceData.setChannelName(entity.getChannelName());
            statisticProduceData.setUser(entity.getUser());
            statisticProduceData.setCooperativePartiner(entity.getCooperativePartiner());
            statisticProduceData.setLiveBeginTime(Timestamp.valueOf(entity.getLiveBeginTime()));
            statisticProduceData.setLiveEndTime(Timestamp.valueOf(entity.getLiveEndTime()));
            statisticProduceData.setDotSubTime(new Timestamp(System.currentTimeMillis()));  //  用本服务器当前时间做为直转点提交时间

            statisticProduceData.setAlbumName(entity.getAlbumName());
            statisticProduceData.setStartAutoDot(entity.getStartAutoDot());
            statisticProduceData.setStartManualDot(entity.getStartManualDot());
            statisticProduceData.setEndAutoDot(entity.getEndAutoDot());
            statisticProduceData.setEndManualDot(entity.getEndManualDot());
            statisticProduceData.setAdInfo(entity.getAdInfo());

            //2017-05-02 add
            statisticProduceData.setOcrFlag(entity.getOcrFlag());
            statisticProduceData.setOcrTitleFlag(entity.getOcrTitleFlag());
            if (entity.getIntelligentInsertTime() != null) {
                statisticProduceData.setIntelligentInsertTime(Timestamp.valueOf(entity.getIntelligentInsertTime()));
            }
            statisticProduceData.setDataMode(entity.getDataMode());
            statisticService.saveProduceData(statisticProduceData);
        } catch (Exception e) {
            logger.error("[StatisticWebServiceImpl][saveProduceData]", e);
            return -1;
        }
        return statisticProduceData.getId();
    }

    @Override
    public TStatisticProduceDataPage getProduceData(TPage page, TFilter filter) throws TException {
        StatisticService statisticService = (StatisticService) ApplicationContextUtil.getBean(StatisticService.class);
        return statisticService.getProduceData(page, filter);
    }

    @Override
    public String saveOcrData(TStatisticOcrData entity) throws TException {

        StatisticOcrDataService service = (StatisticOcrDataService) ApplicationContextUtil.getBean(StatisticOcrDataService.class);

        StatisticOcrData statisticOcrData = new StatisticOcrData();
        statisticOcrData.setChannelId(entity.getChannelId());
        statisticOcrData.setChannelName(entity.getChannelName());
        statisticOcrData.setProgramId(entity.getProgramId());
        statisticOcrData.setProgramName(entity.getProgramName());
        statisticOcrData.setCategoryName(entity.getCategoryName());
        statisticOcrData.setOcrReturnTitle(entity.getOcrReturnTitle());
        statisticOcrData.setUserSubmitTitle(entity.getUserSubmitTitle());
        statisticOcrData.setUser(entity.getUser());
        statisticOcrData.setSameField();

        service.save(statisticOcrData);

        return Constants.CODE_SUC;
    }

    @Override
    public TStatisticOcrDataPage getOcrData(TPage page, TFilter filter) throws TException {
        StatisticOcrDataService service = (StatisticOcrDataService) ApplicationContextUtil.getBean(StatisticOcrDataService.class);
        return service.findTStatisticOcrDataPage(page, filter);
    }

    @Override
    public String saveSty2Data(TStatisticSty2Data entity) throws TException {
        StatisticSty2DataService service = (StatisticSty2DataService) ApplicationContextUtil.getBean(StatisticSty2DataService.class);

        StatisticSty2Data statisticSty2Data = new StatisticSty2Data();
        statisticSty2Data.setChannelId(entity.getChannelId());
        statisticSty2Data.setChannelName(entity.getChannelName());
        statisticSty2Data.setChannelCode(entity.getChannelCode());
        statisticSty2Data.setProgramId(entity.getProgramId());
        statisticSty2Data.setProgramName(entity.getProgramName());
        statisticSty2Data.setCategoryName(entity.getCategoryName());
        statisticSty2Data.setAlbumName(entity.getAlbumName());
        statisticSty2Data.setProgramTime(entity.getProgramTime());
        statisticSty2Data.setHeaderCompleteNumbers(Integer.parseInt(entity.getHeaderCompleteNumbers()));
        statisticSty2Data.setHeaderIncompleteNumbers(Integer.parseInt(entity.getHeaderIncompleteNumbers()));
        statisticSty2Data.setHeaderFaultNumbers(Integer.parseInt(entity.getHeaderFaultNumbers()));
        statisticSty2Data.setLastCompleteNumbers(Integer.parseInt(entity.getLastCompleteNumbers()));
        statisticSty2Data.setLastIncompleteNumbers(Integer.parseInt(entity.getLastIncompleteNumbers()));
        statisticSty2Data.setLastFaultNumbers(Integer.parseInt(entity.getLastFaultNumbers()));
        statisticSty2Data.setAdCompleteNumbers(Integer.parseInt(entity.getAdCompleteNumbers()));
        statisticSty2Data.setAdIncompleteNumbers(Integer.parseInt(entity.getAdIncompleteNumbers()));
        statisticSty2Data.setAdFaultNumbers(Integer.parseInt(entity.getAdFaultNumbers()));
        statisticSty2Data.setUser(entity.getUser());

        service.save(statisticSty2Data);
        return Constants.CODE_SUC;
    }

    @Override
    public TStatisticSty2DataPage getSty2Data(TPage page, TFilter filter) throws TException {
        StatisticSty2DataService service = (StatisticSty2DataService) ApplicationContextUtil.getBean(StatisticSty2DataService.class);
        return service.findTStatisticSty2DataPage(page, filter);
    }

    @Override
    public List<TStatisticSty2PercentData> getSty2PercentData(TPage page, TFilter filter) throws TException {
        StatisticSty2DataService service = (StatisticSty2DataService) ApplicationContextUtil.getBean(StatisticSty2DataService.class);
        return service.findTStatisticSty2PercentData(page, filter);
    }

    @Override
    public List<String> suggestFieldData(TPage page, TFilter filter) throws TException {
        StatisticSty2DataService service = (StatisticSty2DataService) ApplicationContextUtil.getBean(StatisticSty2DataService.class);
        return service.suggest(page, filter);
    }

    @Override
    public long saveOcrNewData(TStatisticOcrNewData entity) throws TException {
        StatisticOcrNewData data = new StatisticOcrNewData();

        data.setLegoProgramId(entity.getLegoProgramId());
        data.setChannelName(entity.getChannelName());
        data.setLiveBeginTime(Timestamp.valueOf(entity.getLiveBeginTime()));
        data.setLiveEndTime(Timestamp.valueOf(entity.getLiveEndTime()));

        data.setImageUrl(entity.getImageUrl());
        data.setLocations(entity.getLocations());
        data.setOcrFlag(entity.getOcrFlag());

        data.setOcrTitle(entity.getOcrTitle());
        data.setOcrTitleCharLen(entity.getOcrTitleCharLen());
        data.setUserTitle(entity.getUserTitle());
        data.setUsrTitleCharLen(entity.getUserTitleCharLen());

        data.setUser(entity.getUser());
        data.setPercent(entity.getPercent());
        long ret = -1L;
        try {
            ret = getStatisticOcrNewDataService().save(data);
        } catch (Exception e) {
            logger.error("saveOcrNewData" + entity.getUser(), e);
        }
        return ret;
    }

    @Override
    public TStatisticOcrNewDataPage getOcrNewData(TPage page, TFilter filter) throws TException {
        TStatisticOcrNewDataPage ret = null;
        try {
            ret = getStatisticOcrNewDataService().findTStatisticOcrNewDataPage(page, filter);
        } catch (Exception e) {
            logger.error("getOcrNewData", e);
        }
        return ret;
    }

    @Override
    public String getOcrAvg(TFilter filter) throws TException {
        String ret = "";
        try {
            ret = getStatisticOcrNewDataService().getOcrNewAvgRate(filter);
        } catch (Exception e) {
            logger.error("getOcrAvg", e);
        }
        return ret;
    }

    @Override
    public String getOcrFullPercentRate(TFilter filter) throws TException {
        String ret = "";
        try {
            ret = getStatisticOcrNewDataService().getOcrFullPercentRate(filter);
        } catch (Exception e) {
            logger.error("getOcrAvg", e);
        }
        return ret;
    }

    @Override
    public long saveAutoNewData(TStatisticAutoNewData entity) throws TException {

        StatisticAutoNewData data = new StatisticAutoNewData();
        data.setLegoProgramId(entity.getLegoProgramId());
        data.setChannelName(entity.getChannelName());
        data.setLiveBeginTime(Timestamp.valueOf(entity.getLiveBeginTime()));
        data.setLiveEndTime(Timestamp.valueOf(entity.getLiveEndTime()));
        data.setUserTitle(entity.getUserTitle());
        data.setUsrTitleCharLen(entity.getUserTitleCharLen());
        data.setUser(entity.getUser());
        data.setPercent(entity.getPercent());
        data.setAutoTitle(entity.getAutoTitle());
        data.setAutoFlag(entity.getAutoFlag());
        data.setAutoTitleCharLen(entity.getAutoTitleCharLen());
        data.setUserTitleSelect(entity.getUserTitleSelect());
        data.setAutoTitleStart(entity.getAutoTitleStart());
        data.setTitleError(entity.getTitleError());
        data.setAutoTitleEnd(entity.getAutoTitleEnd());
        long ret = -1L;
        try {
            ret = getStatisticAutoNewDataService().save(data);
        } catch (Exception e) {
            logger.error("saveOcrNewData" + entity.getUser(), e);
        }
        return ret;
    }

    @Override
    public TStatisticAutoNewDataPage getAutoNewData(TPage page, TFilter filter) throws TException {
        TStatisticAutoNewDataPage ret = null;
        try {
            ret = getStatisticAutoNewDataService().findTStatisticAutoNewDataPage(page, filter);
        } catch (Exception e) {
            logger.error("getAutoNewData", e);
        }
        return ret;
    }

    @Override
    public TResponse getAutoDateRate(TFilter filter) throws TException {
        TResponse tResponse = new TResponse();
        tResponse.setCode(Constants.CODE_ERROR);
        JSONObject jsonObject = new JSONObject();
        String queryKey = null;
        try {
            jsonObject.putAll(getStatisticAutoNewDataService().getAutoOtherDataRate(filter));
            tResponse.setCode(Constants.CODE_SUC);
        } catch (Exception e) {
            logger.error("getAutoDateRate key=" + queryKey, e);
        }
        tResponse.setJsonData(jsonObject.toJSONString());
        return tResponse;
    }
}
